<template>
  <div>
    <!-- <div class="keyboardMask" onTouchMove={this.preventTouchMove}></div> -->
    <div class="keyboard keyboardOpen" onTouchMove={this.preventTouchMove}>
      <div v-if='openBoard||openBoard1' class="close">
        <div @click='close'>关闭</div>
      </div>
      <div class="keyboardLayout" v-if='openBoard'>
        <span v-for="(item, i) in level1" :key="i" class="btnKey">
          <button class="button" type="button" @click.prevent="selectLevel1(item)">
            {{item}}
          </button>
          <div class="btnActive">
            <span>{{item}}</span>
          </div>
        </span>
      </div>
      <div class="keyboardLayout" v-if='openBoard1&&!showFlag'>
        <span v-for="(item, i) in level2.split('')" :key="i" class="btnKey">
          <button class="button" type="button" @click.prevent="selectLevel2(item)">
            {{item}}
          </button>
          <div class="btnActive">
            <span>{{item}}</span>
          </div>
        </span>
        <span class="btnKey backspace">
          <button class="button" type="button" @click="backspace">
            <img src="./font/delete.png" alt="">
          </button>
        </span>
      </div>
    </div>
  </div>
</template>

<script>
export default {
  props: {
    level1: {
      type: String,
      default: '粤京津泸鲁冀云辽黑湘皖苏浙赣鄂桂甘晋蒙陕闽贵青藏川宁琼豫渝台港澳'
    },
    level2: {
      type: String,
      default: '1234567890QWERTYUPASDFGHJKLZXCVBNM'
    },
    openLevel1:{
      type: Boolean,
      default:false
    },
    openLevel2:{
      type: Boolean,
      default:false
    },
    maxLength: {
      type: Number,
      default: 7
    },
    defaultValue: {
      type: String,
      default: ''
    },
    flag:{
      type: Boolean,
      default: false
    },
    keyFlag:{
      type: Boolean,
      default: false
    },
    show4:{
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      winH: window.innerHeight,
      inputValue: this.defaultValue || '',
      openBoard:'',
      openBoard1:'',
      showFlag:''
    };
  },
  watch: {
    defaultValue() {
      this.inputValue = this.defaultValue;
    },
    //来修改父组件控制子组件的显示方式
    openLevel1(newVal,oldVal){
      this.openBoard=this.openLevel1;
    },
    //来修改父组件控制子组件的显示方式
    openLevel2(){
      this.openBoard1=this.openLevel2;
    },
    show4(){
      this.showFlag=this.show4;
    }
  },
  
  methods: {
    //省简称选择
    selectLevel1(level1) {
      this.inputValue = level1;
      this.onChange(level1);
      this.openBoard=false;//隐藏简称键盘
      this.openBoard1=true;//显示数字和字母键盘
      this.showFlag=false;
    },
    //获取选择的值
    onChange(val){
      this.inputValue=val;
      if(this.openLevel1){
        this.$emit('change',val);
      }else{
        this.$emit('brand',val);
      }
      
    },
    //数字和字母键盘选择函数
    selectLevel2(level2) {
      const inputValue = this.inputValue + level2;
      this.inputValue = inputValue;
      this.onChange(inputValue);
    },
    //删除按钮
    backspace() {
      const inputValue = this.inputValue;
      const backspaceValue = inputValue.substr(0, inputValue.length - 1);
      if(backspaceValue.length==0&&this.flag){
        this.openBoard=true;//显示简称键盘
        this.openBoard1=false;//隐藏数字和字母键盘
      }
      this.inputValue = backspaceValue;
      this.onChange(backspaceValue);
    },
    //关闭键盘
    close(){
      this.openBoard=false;//显示简称键盘
      this.openBoard1=false;//
      this.$emit('broadcast')
    }
  }
};
</script>

<style lang="less">
@font-face {
  font-family: "iconfont";
  src: url('./font/iconfont.woff') format('woff'),
  url('./font/iconfont.ttf') format('truetype'),
  url('./font/iconfont.svg#iconfont') format('svg');
}

.iconfont {
  font-family: "iconfont" !important;
  font-size: 2rem;
  font-style: normal;
  -webkit-font-smoothing: antialiased;
  -webkit-text-stroke-width: 0.2px;
  -moz-osx-font-smoothing: grayscale;
}

.icon-backspace:before {
  content: "\e600";
}
.close{
  display: flex;
  justify-content: flex-end;
  padding: 0 0.2rem 0.1rem;
  line-height: 0.6rem;
  div{
    height: 0.6rem;
    color: #fff;
    font-size: 0.35rem;
    
  }
}
.keyboardMask {
  position: absolute;
  z-index: 100;
  top: 36px;
  right: 0;
  bottom: 0;
  left: 0;
  background-color: transparent;
}

.keyboard {
  position: fixed;
  z-index: 900;
  right: 0;
  bottom: 0;
  left: 0;
  padding-top: 0rem;
  user-select: none;
  transition: transform 200ms ease-out;
  transform: translateY(100%);
  text-align: center;
  background-color: #595959;
}

.keyboardOpen {
  transform: translateY(0);
}

.keyboardLayout {
  height: 100%;
  display: flex!important;
  flex-wrap: wrap;
  justify-content: center;
}

.btnKey {
  position: relative;
  display: inline-block;
  width: 0.8rem;
  height: 0.8rem;
}

.button {
  font-size:0.4rem;
  display: block;
  width: 0.7rem;
  height: 0.7rem;
  margin: 0 auto;
  padding: 0;
  text-align: center;
  border: 0;
  border-radius: 3px;
  background-color: #fff;
  color: #000;
  box-shadow: 0 2px 3px rgba(0, 0, 0, .15);
  &:focus {
    outline: 0;
  }
  &:active~.btnActive {
    font-size: 2.5rem;
    line-height: 200%;
    position: absolute;
    top: 0;
    left: -10%;
    display: table;
    width: 120%;
    height: 100%;
    margin-bottom: 10px;
    transform: translateY(-100%);
    border-radius: 5px;
    background-color: #fff;
    box-shadow: 0 2px 3px rgba(0, 0, 0, .15);
    span {
      display: table-cell;
      width: 100%;
      text-align: center;
      vertical-align: middle;
    }
    &::after {
      position: absolute;
      bottom: 0;
      left: 50%;
      width: 0;
      height: 0;
      content: '';
      transform: translate3d(-50%, 100%, 0);
      border-top: 10px solid #fff;
      border-right: 10px solid transparent;
      border-left: 10px solid transparent;
    }
  }
  i {
    font-size: 3rem !important;
    color: #fff;
  }
}

.btnActive {
  display: none;
}

.hide {
  display: none;
}

.show {
  display: block;
}

.backspace {
  width: 1rem;
  vertical-align: middle;
  background: #fff;
  height: 0.7rem;
  border-radius: 0.1rem;
  margin-left: 0.1rem;
  button{
    margin:0;
  }
  img{
    width: 0.6rem;
    margin-top: 0.1rem;
    margin-left: 0.15rem;
  }
  i {
    font-size: 2.5rem;
  }
  
}

.keyboardHide {
  display: none;
}

.keyboardShow {
  display: block;
}
</style>
